package com.javatip.project.queue.controller;

import com.javatip.project.queue.domain.Task;
import com.javatip.project.queue.service.QueueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.javatip.project.queue.domain.Task;

import java.sql.Timestamp;
import java.util.List;

@Controller
@RequestMapping("/queue")
public class QueueController {

    private String prefix ="/queue";
    @Autowired
    private QueueService queueService;

//    @GetMapping("/")
//    public String showQueue(Model model) {
//        List<Queue> tasks = queueService.getAllTasksInOrder();
//        model.addAttribute("tasks", tasks);
//        return "queue";
//    }

    @PostMapping("/addTask/{taskName}/{taskType}/{taskID}")
    public String addTask(@PathVariable("taskName") String taskName, @PathVariable("taskType") String taskType, @PathVariable("taskID") int taskID) {
        Task task = new Task(taskName, taskType, taskID, new Timestamp(System.currentTimeMillis()));
        queueService.saveTask(task);
        return prefix + "/addTask";
    }

    @GetMapping("/removeFirstTask")
    public String removeFirstTask() {
        queueService.removeFirstTask();
        return "redirect:/";
    }

    @GetMapping("/deleteTask")
    public String deleteTask(@RequestParam String taskType, @RequestParam int taskID) {
        queueService.deleteTaskByTypeAndID(taskType, taskID);
        return "redirect:/";
    }
}